package com.hunter.common.util.document;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.log4j.Appender;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.PatternLayout;
import org.docx4j.Docx4J;
import org.docx4j.convert.out.FOSettings;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Word2PdfUtils {
	public static Logger logger = LoggerFactory.getLogger(Word2PdfUtils.class);

	public static void wordToPdf(String wordPath, String pdfPath) {
//		Level level = Level.toLevel(40000);
//		LogManager.getRootLogger().setLevel(level);
//		Appender stdout = new ConsoleAppender();
//		Layout layout = new PatternLayout("%d %p [%c] - %m%n");
//		stdout.setLayout(layout);
//		LogManager.getRootLogger().addAppender(stdout);
//		LogManager.getLogger("org.docx4j.utils.ResourceUtils").setLevel(level);

		try {

//			long start = System.currentTimeMillis();

			InputStream is = new FileInputStream(new File(wordPath));
			WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is);
//			List sections = wordMLPackage.getDocumentModel().getSections();
//			for (int i = 0; i < sections.size(); i++) {
//				System.out.println("sections Size" + sections.size());
//				wordMLPackage.getDocumentModel().getSections().get(i).getPageDimensions().setHeaderExtent(3000);
//			}
			Mapper fontMapper = new IdentityPlusMapper();

			// PhysicalFont font =
			// PhysicalFonts.getPhysicalFonts().get("Comic Sans MS");
			// fontMapper.getFontMappings().put("Algerian", font);
//			wordMLPackage.setFontMapper(fontMapper);
			// PhysicalFont font =
			// PhysicalFonts.getPhysicalFonts().get("Arial Unicode MS");
			// if (font!=null) {
			// fontMapper.getFontMappings().put("Times New Roman", font);
			// }
			fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
			fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
			fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));
			fontMapper.put("黑体", PhysicalFonts.get("SimHei"));
			fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));
			fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
			fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
			fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
			fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB"));
			fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
			fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
			fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
			fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
			fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));

			wordMLPackage.setFontMapper(fontMapper);
//			PdfSettings pdfSettings = new PdfSettings();
//			PdfConversion conversion = new Conversion(wordMLPackage);
//			OutputStream out = new FileOutputStream(pdfPath);
//			conversion.output(out, pdfSettings);
			OutputStream os = new FileOutputStream(pdfPath);
	        FOSettings foSettings = Docx4J.createFOSettings();
	        foSettings.setWmlPackage(wordMLPackage);
	        Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
//	        Docx4J.toPDF(wordMLPackage, os);
	        
//			System.err.println("Time taken to Generate pdf  " + (System.currentTimeMillis() - start) + "ms");

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
//		wordToPdf("D:\\develop\\apache-tomcat-7.0.62\\webapps\\skyeye-file\\20170725\\11\\201707251110100467.docx", "D:\\develop\\apache-tomcat-7.0.62\\webapps\\skyeye-file\\20170725\\11\\201707251110100467.pdf");
		wordToPdf("D:\\develop\\apache-tomcat-7.0.62\\webapps\\skyeye-file\\templet\\demo_replace.docx", "D:\\develop\\apache-tomcat-7.0.62\\webapps\\skyeye-file\\templet\\demo_replace.pdf");
		
	}
}